<?php

namespace app\api\controller;

use app\admin\model\GradeClass;
use app\admin\model\School;
use app\admin\model\Schoolclass;
use app\api\model\StudentGroup;
use app\common\controller\Api;
use fast\Random;
use think\Request;

class Grade extends Api
{


    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new \app\admin\model\GradeClass();
    }

    public function index()
    {
        $user = $this->auth->getUser();
        $re =  (new Schoolclass())->where("teacher_id",$user['id'])->select();
        $list = [];
        if ($re){
            $class_id_arr = array_column($re,'class_id') ;
            $list = $this->model->whereIn('id',$class_id_arr)->select();
            $userModel = new \app\admin\model\User();
            foreach ($list as $key => $value){
                $studeentNum = $userModel->where(['class_id'=>$value['id'],'type'=>'2','school_id'=>$user['school_id']])->count();
                $vipstudent = $userModel->where(['class_id'=>$value['id'],'type'=>'2','school_id'=>$user['school_id']])->where('vip_end_time','>',time())->count();
                $novipstudent = $userModel->where(['class_id'=>$value['id'],'type'=>'2','school_id'=>$user['school_id']])->where('vip_end_time','<',time())->count();
                $list[$key]['studeentNum'] = $studeentNum;
                $list[$key]['vipstudent'] = $vipstudent;
                $list[$key]['novipstudent'] = $novipstudent;
            }
        }
        $this->success('请求成功',$list);
    }
    public function getStudent(){
        $id = $this->request->param('id');
        $user = $this->auth->getUser();
        $userModel = new \app\admin\model\User();
        $studeent = $userModel->field('id,username,nickname,vip_end_time,vip_type')->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])->select();
        $this->success('请求成功',$studeent);
    }

    public function addGroup(){
        $prams = $this->request->post();
        if(empty($prams['name'])) $this->error('请输入群组名称');
        if(empty($prams['class_id'])) $this->error('请选择班级');
        if(empty($prams['student_ids'])) $this->error('请选择学生');
        $prams['user_id'] = $this->auth->id;
        $model = new StudentGroup();
        if(isset($prams['id']) && !empty($prams['id'])){
            $model = StudentGroup::get($prams['id']);
        }
        $res = $model->save($prams);

        if($res ){
            $id =  isset($prams['id']) && !empty($prams['id']) ? $prams['id'] : $model->getLastInsID();
            $usermodel = new \app\common\model\User();
            $usermodel->whereIn('id',$prams['student_ids'])->update(['class_group_id'=>$id]);
            $this->success('编辑成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function delGroup(){
        $id = $this->request->param('id');
        $res = StudentGroup::where(['id'=>$id])->delete();
        if($res){
            $usermodel = new \app\common\model\User();
            $usermodel->where('class_group_id',$id)->update(['class_group_id'=>null]);
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function editGrade(){
        $gradeClassModel = new GradeClass();
        $id = $this->request->param('id');
        $grade = $gradeClassModel->where(['id'=>$id])->find();
        $res= $grade->save(['name'=>$this->request->param('name')]);
        if ($res) {
            $this->success('编辑成功');
        }else{
            $this->error('编辑失败');
        }
    }

    public function getClassInfo(){
        $id = $this->request->param('id');
        $user = $this->auth->getUser();
        $class = $this->model->with('grade')->where(['id'=>$id])->find();
        $schoolname = School::where(['id'=>$user['school_id']])->value('name');
        $userModel = new \app\admin\model\User();
        $class['schoolname'] = $schoolname;
        $studeentNum = $userModel->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])->count();
        $vipstudent = $userModel->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])->where('vip_end_time','>',time())->count();
        $novipstudent = $userModel->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])->where('vip_end_time','<',time())->count();
        $class['studeentNum'] = $studeentNum;
        $class['vipstudent'] = $vipstudent;
        $class['novipstudent'] = $novipstudent;
        $userModel = new \app\admin\model\User();
        $group = $userModel->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])->group('class_group_id')->column('class_group_id');
        $classgroupModel = new StudentGroup();
        $grouplist = $classgroupModel->whereIn('id',implode(',',$group))->select();
        $newdata = [];
        $newdata[] = [
            'name'=>'未分组',
            'studentNum'=>$userModel->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])
                ->whereNull('class_group_id')->count(),
            'student'=>$userModel->field('id,username,nickname,avatar,vip_end_time,vip_type')
                ->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])
                ->whereNull('class_group_id')->select()
        ];
        foreach ($grouplist as $key => $value){
            $newdata[$key+1]['student'] = $userModel->field('id,username,avatar,nickname,vip_end_time,vip_type')
                    ->where(['class_id'=>$id,'type'=>'2','school_id'=>$user['school_id']])
                    ->where(['class_group_id'=>$value['id']])
                    ->select();
            $newdata[$key+1]['studentNum'] = 1;
            $newdata[$key+1]['name']=$value['name'];
            $newdata[$key+1]['id']=$value['id'];
        }

        $class['group'] = $newdata;
        $this->success('请求成功',$class);
    }

    public function getGroup(){
        $user = $this->auth->getUser();
        $list = StudentGroup::where(['user_id'=>$user['id']])->select();
        $userModel = new \app\admin\model\User();
        foreach ($list as $key => $value){
            $list[$key]['studentNum'] = $userModel
                ->where(['class_id'=>$value['class_id'],'type'=>'2','school_id'=>$user['school_id']])
                ->count();
            $list[$key]['student'] = $userModel->field('id,username,nickname,vip_end_time,vip_type')
                ->where(['class_id'=>$value['class_id'],'type'=>'2','school_id'=>$user['school_id']])
                ->select();
        }
        $this->success('请求成功',$list);
    }

    public function getGroupStudent(){
        $id = $this->request->param('id');
        $user = $this->auth->getUser();
        $model = new StudentGroup();
        $userModel = new \app\admin\model\User();
        $group = $model->where(['user_id'=>$user['id'],'id'=>$id])->find();
        $studeent = $userModel->field('id,username,nickname,vip_end_time,vip_type')
            ->where(['class_id'=>$group['class_id'],'type'=>'2','school_id'=>$user['school_id']])
            ->select();

        $this->success('请求成功',$studeent);
    }

    public function resetpassword(){
        $id = $this->request->param('id');
        $user = \app\admin\model\User::get($id);
        if (empty($user)) $this->error('用户不存在');
        $newpasswordyuan = Random::alnum(8);
        $salt = Random::alnum();
        $newpassword = $this->getEncryptPassword($newpasswordyuan, $salt);
        $ret = $user->save(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]);
        if($ret){
            $this->success('重置成功',['password'=>$newpasswordyuan]);
        }else $this->error('重置失败');
    }

    public function getEncryptPassword($password, $salt = '')
    {
        return md5(md5($password) . $salt);
    }
}